import { Asset, Material, Prefab, SpriteFrame, resources } from "cc";
import { EntityTypeEnum, Singleton } from "../Common";

export default class ResourcesManager extends Singleton {

    static get Ins() {
        return super.GetInstance<ResourcesManager>()
    }

    textureMap: Map<EntityTypeEnum, SpriteFrame[]> = new Map()
    prefabMap: Map<EntityTypeEnum, Prefab> = new Map()
    materialMap: Map<string, Material> = new Map()

    loadRes<T extends Asset>(path: string, type: new (...args: any[]) => T) {
        return new Promise<T>((resolve, reject
        ) => {
            resources.load(path, type, (err, data) => {
                if (err) {
                    console.error('### bundle doesn‘t have contain' + path)
                    reject()
                    return
                }
                resolve(data)
            })
        })
    }

    loadDir<T extends Asset>(path: string, type: new (...args: any[]) => T) {
        return new Promise((resolve, reject) => {
            resources.loadDir(path, type, (err, data) => {
                if (err) {
                    console.error(err)
                    reject()
                    return
                }
                resolve(data)
            })
        })
    }

}